// pages/appiointChoose/dateAndSourceChoose.js
const util = require("../../utils/util")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    scrollLeft:0,
    schedulingList: [],
    currentSchedulingList: [],
    dateList: [],
    drCode: '',
    deptCode: '',
    nowDate: ''
  },
  tabSelect(e) {
    this.data.currentSchedulingList = this.data.schedulingList.filter(m=>m.date ==e.currentTarget.dataset.target)
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id-1)*60,
      currentSchedulingList: this.data.currentSchedulingList
    })
  },
  handleAppoint(event){
    let that = this
    wx.navigateTo({
      url: '/pages/appiointChoose/timeAndCardChoose?scheid='+event.currentTarget.dataset.scheid+"&ape="+event.currentTarget.dataset.target,
      success: function(res){
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('data', { schedulingList: that.data.schedulingList,dateList: that.data.dateList })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that  = this;
    that.data.dateList.splice(0,that.data.dateList.length);
    that.data.nowDate =util.timestampToLocalFormat(util.dateToTimestamp(new Date))
    that.setData({
      nowDate: that.data.nowDate
    })
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('schedulingList', function(data){
      that.data.schedulingList = data.data
    });
    that.data.drCode = options.drCode;
    that.data.deptCode = options.deptCode;
    if(options.drCode !== '' && options.drCode !== undefined && options.drCode !== null){
      that.data.schedulingList = that.data.schedulingList.filter(m=>m.drCode == options.drCode)
    } else if(options.deptCode !== '' && options.deptCode !== undefined && options.deptCode != null){
      that.data.schedulingList = that.data.schedulingList.filter(m=>m.deptCode == options.deptCode)
    }

    that.data.schedulingList.forEach(item=>{
      if(that.data.dateList.findIndex(m=>m==item.date)<0){
        that.data.dateList.push(item.date)
      }
    })
    if(that.data.dateList.length>0){
      let curSchedulings = that.data.schedulingList.filter(m=>m.date==that.data.dateList[0]);
      that.setData({
        dateList: that.data.dateList,
        currentSchedulingList: curSchedulings
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})